/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaModem.java
 *
 * Created on 31/01/2012, 13:43:54
 */
package tsb.UI.Ventanas;

import java.util.ArrayList;
import java.util.Iterator;
import tsb.dominio.dispositivos.Dispositivo;
import tsb.dominio.dispositivos.coneccion.Modem;
import tsb.dominio.excepciones.MACException;
import tsb.dominio.interfaces.FastEthernet;
import tsb.dominio.interfaces.IP;
import tsb.dominio.interfaces.InterfaceDeConeccion;
import tsb.dominio.interfaces.LineaTelefonica;
import tsb.dominio.interfaces.MAC;
import tsb.dominio.interfaces.RJ45FastEthernet;

/**
 *
 * @author Fede
 */
public class VentanaModem extends javax.swing.JFrame
{
    private Modem modem;
    private ArrayList<InterfaceDeConeccion> listaInterfaces;
    private tsb.gestores.GestorDeDispositivos gestorDisp;
    private tsb.UI.PanelArrastrar.BotonPersonalizado btnPersonalizado;
   
    public VentanaModem(String nombreAMostrar, tsb.gestores.GestorDeDispositivos gestorDisp, Dispositivo disp,tsb.UI.PanelArrastrar.BotonPersonalizado btnPersonalizado) throws MACException
    {
        initComponents();
        this.setLocationRelativeTo(null);
        modem=(Modem) disp;
        this.setTitle(modem.getNombreAMostrar());
        if(modem.getInterfaces()==null)
        {
            listaInterfaces= new ArrayList<InterfaceDeConeccion>();
            IP ip = new IP();
            IP subNet= new IP();
            MAC mac= new MAC("8A-CE-34-61-25-F9");
            RJ45FastEthernet fe= new RJ45FastEthernet(true,1200,ip,subNet,true,true, mac);
            listaInterfaces.add(fe);
            this.modem.setInterfaces(listaInterfaces);
            IP ip2= new IP();
            IP subNet2= new IP();
            MAC mac2= new MAC("8A-CE-34-61-25-F8");
            LineaTelefonica lt= new LineaTelefonica(true,1200,ip2,subNet2,true,true, mac2);
            listaInterfaces.add(lt);
        }
        else
        {
            listaInterfaces = modem.getInterfaces();
        }
        this.gestorDisp=gestorDisp;
        
        this.btnPersonalizado=btnPersonalizado;
       
//        if(this.listaInterfaces==null)
//        {
//            listaInterfaces.add(fe);
//            this.modem.setInterfaces(listaInterfaces);
//        }

       
        this.setModem(modem);
    }
    public void setModem(Modem modem) throws MACException
    {
        this.modem=modem;
        this.panelModem.setNombreAMostrar(modem.getNombreAMostrar());
        this.panelModem.setEncendido(modem.getEstado());
        if(modem.getEstado())
        {
            panelModem.setEthernet(true);
        }
        else
        {
            panelModem.setEthernet(false);
        }
        if(modem.getInterfaces()!=null)
        {
             RJ45FastEthernet i=(RJ45FastEthernet)this.listaInterfaces.get(0);

              if(i.getEstado()==true)
               {
                 this.panelModem.setEthernet(true);
                 this.panelModem.setComboEthernet(true);
               }
               else
               {
                  this.panelModem.setEthernet(false);
                  this.panelModem.setComboEthernet(false);
               }

             LineaTelefonica lt= (LineaTelefonica)this.listaInterfaces.get(1);
             if(lt.getEstado()==true)
             {
                 this.panelModem.setLineaTelefonica(true);
                 this.panelModem.setComboLineaTelefonica(true);
             }
             else
             {
                 this.panelModem.setLineaTelefonica(false);
                 this.panelModem.setComboLineaTelefonica(false);
             }
        }
        
 
    }
    public Modem getModem()
    {
        this.modem.setNombreAMostrar(this.panelModem.getNombreAMostrar());
        this.modem.setEstado(this.panelModem.getEncendido());
        this.listaInterfaces=this.modem.getInterfaces();
        if(this.listaInterfaces!=null)
        {
              RJ45FastEthernet i=(RJ45FastEthernet)this.listaInterfaces.get(0);

              if(this.panelModem.getEthernet())
              {
                i.setEstado(true);
              }
              else
              {
                i.setEstado(false);
              }
              LineaTelefonica lt= (LineaTelefonica)this.listaInterfaces.get(1);
              if(this.panelModem.getLineaTelefonica())
              {
                  lt.setEstado(true);
              }
              else
              {
                  lt.setEstado(false);
              }

        }


     


//      
      
//        if(this.modem.getFastE())
//        {
//            this.panelModem.setEthernet(true);
//        }
//        else
//        {
//            this.panelModem.setEthernet(false);
//        }
//        if(this.modem.getLineaTel())
//        {
//            this.panelModem.setLineaTelefonica(true);
//        }
//        else
//        {
//            this.panelModem.setLineaTelefonica(false);
//        }
//        if(panelModem.getPestaña1()==true)
//        {
//            this.listaInterfaces.add(this.panelModem.getInterfaz1());
//            this.modem.setInterfaces(listaInterfaces);
//        }
//        if(panelModem.getPestaña2()==true)
//        {
//            this.listaInterfaces.add(this.panelModem.getInterfaz2());
//            this.modem.setInterfaces(listaInterfaces);
//        }

        return modem;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelModem = new tsb.UI.Paneles.PanelModem();
        panelBotones = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modem");

        panelBotones.setBackground(new java.awt.Color(255, 255, 255));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addContainerGap(223, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAceptar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelModem, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
            .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelModem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        agregarDisp();
    }//GEN-LAST:event_btnAceptarActionPerformed
  private void agregarDisp()
    {
         Modem modem= this.getModem();
         gestorDisp.actualizarDisp(modem);
         btnPersonalizado.setText(modem.getNombreAMostrar());
         this.btnPersonalizado.setVentanaAbierta(false);
         this.dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JPanel panelBotones;
    private tsb.UI.Paneles.PanelModem panelModem;
    // End of variables declaration//GEN-END:variables
}
